package com.example.springaop3.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-03-13
 * Time: 23:29
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String username, String password){
        if(StringUtils.hasLength(username)&&StringUtils.hasLength(password)){
            if("admin".equals(username)&&"admin".equals(password)){
                HttpSession session=request.getSession(true);
                session.setAttribute("userinfo", "userinfo");
                return "已登录";
            }
        }
        return "未登录";
    }

    @RequestMapping("/index")
    public String index(){
        return "hello, index";
    }

    @RequestMapping("/exception")
    public String exception(){
        // 算数异常
        // int a=10/0;
//        如果出现的异常类型与要拦截的异常类型不一致, 那么拦截不到
        Object object=null;
        System.out.println(object.hashCode());
        return "no exception";
    }
}
